package com.ddyx.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.ddyx.entity.wxGood;
import com.ddyx.entity.wxGoodsImage;
import com.ddyx.mapper.wxGoodMapper;
import com.ddyx.mapper.wxGoodsImageMapper;
import com.ddyx.service.IWxClientGoodService;
import com.ddyx.util.DateUtil;
import com.ddyx.util.Page;
import com.ddyx.util.PageUtil;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class WxClientGoodServiceImpl implements IWxClientGoodService{

	@Resource
	wxGoodMapper wxgoodMapper;
	@Resource 
	wxGoodsImageMapper wxgoodsImageMapper;
	
	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm selectwxGood(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String,Object> paramMap = (Map<String,Object>)param.getData();
		try {
			List<wxGood> goodList = wxgoodMapper.selectwxGood(Page.parse(paramMap));
			result.setData(goodList);
			int total = wxgoodMapper.selectwxGoodCount(paramMap);
			result.setTotal(total);
		} catch (Exception e) {
			result.setStatus(false);
			result.setMessage("查询商品列表错误");
			log.error(e+"查询商品列表错误");
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm selectwxGoodInfoById(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String,Object> paramMap = (Map<String,Object>)param.getData();
		try {
			wxGood goods = wxgoodMapper.selectwxGoodInfoById(paramMap);
			
			List<wxGoodsImage> goodsPics = wxgoodsImageMapper.selectwxGoodsImage(goods.getGoodsId());
			if(goodsPics!=null)
				goods.setGoodsPics(goodsPics);
			goods.setIsLike(0);
			goods.setIsFavorite(0);
			//获取用户对商品的点赞和收藏记录
			List<Map<String,Object>> events = wxgoodMapper.getGoodsFootprint(paramMap);
			if(events!=null)
			{
				for(Map<String,Object> map:events)
				{
					Object obj = map.get("action_type");
					if(obj!=null)
					{
						Integer action = Integer.parseInt(obj.toString());
						if(action==1)//收藏
							goods.setIsFavorite(1);
						if(action==2)//点赞
							goods.setIsLike(1);
					}
				}
			}
			result.setData(goods);
		} catch (Exception e) {
			result.setStatus(false);
			result.setMessage("查询商品详情错误");
			log.error(e+"查询商品详情错误");
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ResponseForm insertwxGood(RequestForm param) {
		ResponseForm result = new ResponseForm();
		Map<String,Object> paramMap = (Map<String,Object>)param.getData();
		try {
			paramMap.put("createTime", DateUtil.format_ymd_hms.format(new Date()));
			int index = wxgoodMapper.insertwxGood(paramMap);
			if(index < 0) {
				result.setStatus(false);
				result.setMessage("加入商品失败");
			}
		} catch (Exception e) {
			result.setStatus(false);
			result.setMessage("查询商品详情错误");
			log.error(e+"查询商品详情错误");
		}
		return result;
	}

}
